<template>
  <div class="app-container" style="font-size: 14px;font-weight: 500">
    <!-- $t is vue-i18n global function to translate lang -->

    <el-button type="primary" size="small" @click="hrefTo">
      <svg-icon icon-class="add_table" class-name="card-panel-icon icon-refresh" />
    </el-button>
    <el-button type="primary" size="small" @click="getList">
      <svg-icon icon-class="refresh" class-name="card-panel-icon icon-refresh" />
    </el-button>
    <el-button :loading="downloadLoading" size="small" style="margin-bottom:20px" type="primary" icon="el-icon-document" @click="handleDownload">
      {{ $t('excel.selectedExport') }}
    </el-button>
    <el-table
      ref="multipleTable"
      v-loading="listLoading"
      :element-loading-text="$t('table.load')"
      :data="list"
      stripe
      fit
      size="medium"
      highlight-current-row
      @selection-change="handleSelectionChange"
    >

      <el-table-column type="selection" align="center" />
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('task.title')">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('task.total_num')">
        <template slot-scope="scope">
          {{ scope.row.total_num }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('task.succeed_num')">
        <template slot-scope="scope">
          {{ scope.row.succeed_num }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('task.fail_num')">
        <template slot-scope="scope">
          {{ scope.row.fail_num }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('user.number')">
        <template slot-scope="scope">
          {{ scope.row.total_price }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('task.finish_at')">
        <template slot-scope="scope">
          {{ scope.row.finish_at }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('task.created_at')">
        <template slot-scope="scope">
          {{ scope.row.created_at }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('task.progress')">
        <template slot-scope="scope">
          <el-progress :percentage="scope.row.progress"></el-progress>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.actions')" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <template v-if="row.send_status === 3 ">
            <el-button type="primary" size="mini" @click="downloadFile(row)">
              {{ $t('route.down') }}
            </el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-dialog :visible.sync="dialogPvVisible" title="">
      <el-table
        v-loading="listLoading"
        :data="logData"
        border
        fit
        highlight-current-row
        style="width: 100%"
        :element-loading-text="$t('table.load')"
      >
        <el-table-column :label="$t('task.number')">
          <template slot-scope="scope">
            {{ scope.row.number }}
          </template>
        </el-table-column>
        <el-table-column prop="status" :label="$t('task.status')" :formatter="statusFormat" width="150" />
        <el-table-column prop="info" :label="$t('task.info')">
          <template slot-scope="scope">
            {{ scope.row.info }}
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogPvVisible = false">{{ $t('table.confirm') }}</el-button>
      </span>
      <pagination v-show="logTotal>0" :total="logTotal" :page.sync="logListQuery.page" :limit.sync="logListQuery.limit" @pagination="getLogList" />
    </el-dialog>
  </div>
</template>

<script>
import { list } from '@/api/dnd'
import Pagination from '@/components/Pagination'

export default {
  name: 'SelectExcel',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      listLoading: true,
      multipleSelection: [],
      downloadLoading: false,
      filename: '',
      taskId: '',
      logData: [],
      logTotal: 0,
      logListQuery: {
        page: 1,
        limit: 10,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      logListLoading: false,
      dialogPvVisible: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    downloadFile(row) {
      const url = row.down_url; // 文件的 URL 地址
      const link = document.createElement('a');
      link.href = url;
      link.setAttribute('download', ''); // 设置文件属性为下载
      document.body.appendChild(link); // 将元素插入到 DOM 中
      link.click(); // 模拟点击下载链接进行下载
      document.body.removeChild(link); // 下载完成后移除元素
    },
    handleUpdate(row) {
      this.dialogPvVisible = true
      this.logListLoading = true
      this.taskId = row.id
      this.getLogList()
    },
    getLogList() {
      logList({
        taskId: this.taskId,
        page: this.logListQuery.page,
        limit: this.logListQuery.limit
      }).then(response => {
        this.logData = response.data.list
        this.logTotal = response.data.total
        this.listLoading = false
      })
    },
    getList() {
      this.listLoading = true
      list({
        page: this.listQuery.page,
        limit: this.listQuery.limit
      }).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    statusFormat(row, column, cellValue, index) {
      return { 1: 'Create', 2: 'Sucess', 3: 'fail' }[cellValue]
    },
    sendStatusFormat(row, column, cellValue, index) {
      return { 0: 'Waiting to send', 1: 'Waiting to send', 2: 'Underway', 3: 'Underway', 5: 'Sucess', 4: 'Fail' }[cellValue]
    },
    handleDownload() {
      if (this.multipleSelection.length) {
        this.downloadLoading = true
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['Id', 'Title', 'Type', 'Country', 'ReceiverNum', 'SuccessNum', 'FailNum', 'Credits']
          const filterVal = ['id', 'title', 'file_type', 'country_code', 'total_num', 'succeed_num', 'fail_num', 'total_price']
          const list = this.multipleSelection
          const data = this.formatJson(filterVal, list)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: this.filename
          })
          this.$refs.multipleTable.clearSelection()
          this.downloadLoading = false
        })
      } else {
        this.$message({
          message: 'Please select at least one item',
          type: 'warning'
        })
      }
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    hrefTo() {
      this.$router.push('/task/create')
    }
  }
}
</script>
<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 120px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 28%;
}
.icon-refresh {
  color: #ffffff;
}
</style>
